package cn.hhchat.wp.controller;

import cn.hhchat.wp.dto.form.PostForm;
import cn.hhchat.wp.dto.form.PostQueryForm;
import cn.hhchat.wp.dto.msg.RMsg;
import cn.hhchat.wp.dto.msg.RMsgFactory;
import cn.hhchat.wp.dto.msg.RMsgModel;
import cn.hhchat.wp.dto.post.DetailPost;
import cn.hhchat.wp.dto.user.SimpleUser;
import cn.hhchat.wp.entity.JoinRelationEntity;
import cn.hhchat.wp.entity.PostEntity;
import cn.hhchat.wp.entity.GroupEntity;
import cn.hhchat.wp.entity.UserEntity;
import cn.hhchat.wp.exception.UnAuthenticationException;
import cn.hhchat.wp.service.AttachmentService;
import cn.hhchat.wp.service.GroupService;
import cn.hhchat.wp.service.PostService;
import cn.hhchat.wp.service.UserService;
import cn.hhchat.wp.util.DateUtil;
import cn.hhchat.wp.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created this one by HMH on 2017/10/22.
 */
@RestController
@RequestMapping("/post")
public class PostController {

    @PutMapping
    public RMsg createPost(@RequestBody PostForm postForm) {
        UserEntity user = SessionUtil.getUser();
        if (null == user) {
            throw new UnAuthenticationException("未登录");
        }
        GroupEntity group = groupService.getById(postForm.getGroupId());
        PostEntity post = postService.createOne(postForm, user, group);
        return RMsgFactory.ok(post.toSimplePost());
    }

    //获取某个人某周的所有post
    @GetMapping("/getPostOfWeek")
    public RMsg getPostOfWeek(@RequestBody PostQueryForm postQueryForm) {
        UserEntity user = userService.getById(postQueryForm.getUserId());
        int year = postQueryForm.getYear();
        int week = postQueryForm.getWeek();
        GroupEntity group = groupService.getById(postQueryForm.getGroupId());
        List<PostEntity> post = postService.getPostOfUserInWeek(group, user, year, week);
        List<DetailPost> detailPosts = post.stream().map(p->p.toDetailPost(urlRoot)).collect(Collectors.toList());
        return RMsgFactory.ok(detailPosts);
    }

    //重新编辑某个post
    @PostMapping("/reedit")
    public RMsg editPost(@RequestBody PostForm postForm) {
        UserEntity user = SessionUtil.getUser();
        if (null == user) {
            throw new UnAuthenticationException("未登录");
        }
        PostEntity post = postService.getById(postForm.getPostId());
        post = postService.edit(post, postForm);
        return RMsgFactory.ok(post.toSimplePost());
    }

    //获取某个项目组人员post情况
    @GetMapping("/getGroupPostInfoOfWeek")
    public RMsg getGroupPostInfoOfWeek(@RequestParam long groupId, @RequestParam int week , @RequestParam int year) {
        GroupEntity group = groupService.getById(groupId);
        List<UserEntity> allActiveMember = groupService.getMembers(group).stream().map(JoinRelationEntity::getUser).collect(Collectors.toList());
        List<UserEntity> allHadPostedMember = postService.getPostOfGroupInWeek(group, year, week).stream().map(PostEntity::getPoster).distinct().collect(Collectors.toList());
        LocalDate localDate = LocalDate.now();
        List<SimpleUser> allNeedToPost = null;
        List<SimpleUser> allPosted = allHadPostedMember.stream().map(UserEntity::toSimpleUser).collect(Collectors.toList());
        if (year == localDate.getYear() && week == DateUtil.getWeekOfYear(localDate)) {
            allNeedToPost = allActiveMember.stream().map(UserEntity::toSimpleUser).collect(Collectors.toList());
        }else{
            //过去周只显示已提交人
            allNeedToPost = allPosted;
        }
        return RMsgFactory.ok(new RMsgModel()
                .add("allNeedToPost",allNeedToPost)
                .add("allPosted",allNeedToPost).data());

    }

    @GetMapping("/getUserPostOfGroupInWeek")
    public RMsg getMyPostOfGroupInWeek(long groupId, long userId, int year, int week) {
        UserEntity user = userService.getById(userId);
        GroupEntity group = groupService.getById(groupId);
        List<PostEntity> posts = postService.getPostOfUserInWeek(group, user, year, week);
        return RMsgFactory.ok(posts.stream().map(p->p.toDetailPost(urlRoot)).collect(Collectors.toList()));
    }

    @GetMapping("/getMyPostOfGroupInWeek")
    public RMsg getMyPostOfGroupInWeek(long groupId, int year, int week) {
        UserEntity user = SessionUtil.getUser();
        GroupEntity group = groupService.getById(groupId);
        List<PostEntity> posts = postService.getPostOfUserInWeek(group, user, year, week);
        return RMsgFactory.ok(posts.stream().map(p->p.toDetailPost(urlRoot)).collect(Collectors.toList()));
    }

    @GetMapping("/getMyLast5Post")
    public RMsg getMyLast5Post(long groupId){
        UserEntity user = SessionUtil.getUser();
        GroupEntity group = groupService.getById(groupId);
        List<PostEntity> posts = postService.getLastFivePostOfUser(group, user);
        return RMsgFactory.ok(posts.stream().map(p->p.toDetailPost(urlRoot)).collect(Collectors.toList()));
    }


    @Autowired
    private GroupService groupService;

    @Autowired
    private AttachmentService attachmentService;

    @Autowired
    private PostService postService;

    @Autowired
    private UserService userService;

    @Value("${sei.resource.urlRoot}")
    private String urlRoot;


}
